package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.dto.ExpenseDTO;
import io.github.talelin.latticy.service.ExpenseService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

@RestController
@RequestMapping("/api/expense")
@Validated
public class ExpenseController {

    @Autowired
    private ExpenseService expenseService;

    /**
     * 报销
     * @param dto
     * @return
     */
    @PostMapping
    @LoginRequired
    public CreatedVO create(@RequestBody @Validated ExpenseDTO dto) {
        expenseService.create(dto);
        return new CreatedVO<>();
    }

    /**
     * 编辑报销
     */
    @PutMapping("/{id}")
    @LoginRequired
    public UpdatedVO update(@RequestBody @Validated ExpenseDTO dto,
                       @PathVariable @Positive Long id) {
        expenseService.updated(id, dto);
        return new UpdatedVO<>();
    }

}
